package com.woniu.cms.goods.core.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.woniu.cms.goods.core.entity.GoodsGetbill;
import com.woniu.cms.goods.core.entity.GoodsStores;
import com.woniu.cms.goods.core.exception.BusinessException;
import com.woniu.cms.goods.core.mapper.GoodsStoresMapper;
import com.woniu.cms.goods.core.service.IGoodsGetbillService;
import com.woniu.cms.goods.core.service.IGoodsStoresService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniu.common.api.Result;
import com.woniu.common.api.status.IStatusCode;
import com.woniu.common.api.status.Impl.BusinessCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 仓库表 服务实现类
 * </p>
 *
 * @author romantic sheep
 * @since 2022-09-17
 */
@Service
public class GoodsStoresServiceImpl extends ServiceImpl<GoodsStoresMapper, GoodsStores> implements IGoodsStoresService {

    @Autowired
    private IGoodsGetbillService getbillService;

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private ObjectMapper objectMapper;

    /*
    * 分页查询/根据仓库名称搜索方法
    * @currentPage 当前页码
    * @pageSize 页面大小
    * @storeName 用户输入的仓库名称
    * */
    @Override
    public Result getlist(Integer currentPage, Integer pageSize,String storeName) {
        IPage<GoodsStores> iPage =new Page(currentPage,pageSize);
        LambdaQueryWrapper<GoodsStores> qw = Wrappers.lambdaQuery();
        if(StringUtils.hasText(storeName)){
            qw.like(GoodsStores::getStoreName,storeName);
        }
        IPage<GoodsStores> page = this.page(iPage,qw);
        long total = page.getTotal();
        List<GoodsStores> records = page.getRecords();
        Map map=new HashMap();
        map.put("total",total);
        map.put("data",records);
        Result<Map> result = Result.common(map);
        return result;
    }
    /*
    * 新增仓库的方法
    * @stores 用户输入的仓库对象的
    * */
    @Override
    @Transactional( propagation = Propagation.REQUIRED  //必须添加事务
            ,timeout = -1         //设置连接永不超时
            ,rollbackFor = {BusinessException.class}  // 抛出异常时进行回滚
            ,isolation = Isolation.DEFAULT                   // 使用数据库的隔离级别
    )
    public Result addStore(GoodsStores stores) throws JsonProcessingException {
        if(ObjectUtils.isEmpty(stores)){
            return Result.error(BusinessCode.ADD_FAIL);
        }
        boolean save = this.save(stores);
        if (!save){
            return Result.error("仓库已存在");
        }else {
            return Result.error(BusinessCode.ADD_SUCCESS);
        }
    }
    /*
     * 删除仓库的方法，当仓库有被1条或多条材料引用时无法删除
     * @id 仓库id
     * */
    @Override
    @Transactional( propagation = Propagation.REQUIRED  //必须添加事务
            ,timeout = -1         //设置连接永不超时
            ,rollbackFor = {BusinessException.class}  // 抛出异常时进行回滚
            ,isolation = Isolation.DEFAULT                   // 使用数据库的隔离级别
    )
    public Result deleteStore(Integer id) {
        GoodsStores store = this.getById(id);
        if(ObjectUtils.isEmpty(store)){
            return Result.error();
        }
        //先查询该仓库下有无材料，有则返回失败信息无则删除
        LambdaQueryWrapper<GoodsGetbill> qw = Wrappers.lambdaQuery();
        qw.select(GoodsGetbill::getStoreName);
        qw.eq(GoodsGetbill::getStoreName,store.getStoreName());
        GoodsGetbill getbill = getbillService.getOne(qw);
        if (!ObjectUtils.isEmpty(getbill)) {
            return Result.error(BusinessCode.REMOVE_FAIL);
        }
        boolean b = this.removeById(id);
        if(!b){
            return Result.error(BusinessCode.REMOVE_FAIL);
        }
        return Result.ok(BusinessCode.REMOVE_SUCCESS);
    }
}
